package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.SetMealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetMealService;
import com.microsoft.schemas.vml.STTrueFalse;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetMealService.class)
@Transactional
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    private SetMealDao setMealDao;

    @Autowired
    private JedisPool jedisPool;

    @Autowired
    private Configuration configuration;

    @Value(value = "out_put_path")
    private String outPath;

    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {

        setMealDao.save(setmeal);
        /*
         * 拿到存往数据库的img文件名 存储到 --> redis
         * 此处可以直接从setmeal中获取
         */
        String img = setmeal.getImg();
        if (img != null){
            //存储到 redis
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,img);
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,img);
        }

        Integer id = setmeal.getId();

        for (Integer checkgroupId : checkgroupIds) {
            Map<String,Integer> map = new HashMap<>();
            map.put("setmeal_id",id);
            map.put("checkgroup_id",checkgroupId);
            setMealDao.saveInnerTable(map);
        }
        //新建套餐结束后 刷新静态化页面
        try {
            this.makeStaticPages(setmeal);
        } catch (IOException | TemplateException e) {
            e.printStackTrace();
            //TODO 如果页面静态化过程中出现错误 --> 做什么处理
        }
    }

    @Override
    public PageResult findAll(QueryPageBean queryPageBean) {
        Page<Setmeal> setMeals = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        setMealDao.findAll(queryPageBean.getQueryString());
        return new PageResult(setMeals.getTotal(),setMeals);
    }

    @Override
    public void updateSetMeal(Setmeal setmeal,Integer[] checkgroupIds) {

        //有页面更新 需要刷新静态化页面
    }

    @Override
    public List<Setmeal> getAllSetMeal() {
        List<Setmeal> all = setMealDao.findAll(null);
        return all;
    }

    @Override
    public Setmeal findById(Integer id) {
        Setmeal setmeal = setMealDao.findById(id);

        return setmeal;
    }

    private void makeStaticPages(Setmeal setmeal) throws IOException, TemplateException {
        //获取模板位置
        Template template
                = configuration.getTemplate("/WEB-INF/mobile_setmeal_detail.ftl");
        OutputStreamWriter outputStreamWriter
                = new OutputStreamWriter(new FileOutputStream("E:\\.aheima\\soft\\health_parent\\health_mobile\\src\\main\\webapp\\pages\\mobile_setmeal_"+setmeal.getId()+".html"));
        template.process(setmeal,outputStreamWriter);
    }
}